import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
//封装  origin 方法
function origin(target, type) {
  // 保存target   ---  type 方法
  const origin = target.prototype[type];
  //增强 replace 方法
  target.prototype[type] = function (location, okCallback, errCallback) {
    if (okCallback || errCallback) {
      return origin.call(this, location, okCallback, errCallback);
    } else {
      return origin.call(this, location).catch(() => {});
    }
  };
}
//增强push方法
origin(VueRouter, "push");
//增强 replace 方法
origin(VueRouter, "replace");

import routes from "./routers";

//创建路由对象
export default new VueRouter({
  mode: "history",
  routes,
  // 1. 刚进入页面，2.，每次切换路由
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
});
